package com.itheima04;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FuWuQi {
    public static void main(String[] args) throws IOException {


        ServerSocket serverSocket = new ServerSocket(10000);

        ExecutorService executorService = Executors.newCachedThreadPool();
        while (true) {
            Socket accept = serverSocket.accept();

            executorService.submit(() -> {


                BufferedOutputStream bos = null;

                try {
                    BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());

                    UUID uuid = UUID.randomUUID();
                    String s = uuid.toString().replaceAll("-", "");
                    bos = new BufferedOutputStream(new FileOutputStream(s + ".jpg"));


                    int b;
                    while ((b = bis.read()) != -1) {
                        bos.write(b);
                    }

                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
                    bufferedWriter.write("上传成功!");
                    bufferedWriter.newLine();
                    bufferedWriter.flush();


                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            });


        }
//        serverSocket.close();

    }
}
